package cn.yj.core.business.controller;

import cn.yj.annotation.pagehelper.page.OrderBy;
import cn.yj.cgs.dba.entity.Article;
import cn.yj.cgs.dba.entity.vo.ArticleFormVo;
import cn.yj.cgs.dba.entity.vo.ArticleVo;
import cn.yj.syslog.common.LogType;
import cn.yj.syslog.common.OperateLog;
import cn.yj.config.annotation.Anonymous;
import cn.yj.config.web.AppBaseController;
import cn.yj.core.business.service.iservice.IArticleService;
import cn.yj.entity.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 永健
 * @since 2021-04-20 17:42
 */
@RequestMapping("/article")
@RestController
public class ArticleController extends AppBaseController<ArticleVo> {

    @Resource
    private IArticleService iArticleService;

    @Anonymous
    @GetMapping("/pageList")
    public R pageList(@RequestParam Map<String, Object> params) {
        params.put("currentUserCode",getUserCode());
        return success(iArticleService.listPage(params, page(new OrderBy(OrderBy.Direction.DESC, "create_time"))));
    }

    @GetMapping("/myArticles")
    public R myArticles(Map<String, Object> params) {
        params.put("userCode", getUserCode());
        return success(iArticleService.myArticles(params, page()));
    }

    @OperateLog(describe = "发布文章",type = LogType.INSERT)
    @PostMapping("/publishArticle")
    public R pushArticle(@RequestBody ArticleFormVo article) {
        return iArticleService.pushArticle(article.setUserCode(getUserCode()));
    }


    @PostMapping("/deleteImg/{imgId}")
    public R deleteArticleImg(@PathVariable Integer imgId) {
        return result(iArticleService.deleteArticleImg(imgId));
    }

    //@PostMapping("/updateArticle")
    public R updateArticle(@RequestBody Article article) {
        return result(iArticleService.updateArticleByNo(article.setUserCode(getUserCode())));
    }

    /**
     * 点赞
     *
     * @param articleNo 文章编号
     */
    @OperateLog(describe = "文章点赞",type = LogType.UPDATE)
    @PostMapping("/agree/{articleNo}")
    public R agree(@PathVariable("articleNo") String articleNo) {
        return result(iArticleService.agree(getUserCode(), articleNo));
    }

    @PostMapping("/view/{articleNo}")
    public R view(@PathVariable("articleNo") String articleNo) {
        iArticleService.view(getUserCode(), articleNo);
        return success();
    }

    @OperateLog(describe = "删除文章",type = LogType.DELETE)
    @DeleteMapping("/delete/{articleNo}")
    public R deleteByNo(@PathVariable("articleNo") String articleNo) {
        return result(iArticleService.deleteByNo(articleNo));
    }

    static List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();

    static {
        Map<String, Object> type1 = new HashMap<>();
        Map<String, Object> type2 = new HashMap<>();
        Map<String, Object> type3 = new HashMap<>();
        Map<String, Object> type4 = new HashMap<>();

        type1.put("name", "推荐");
        type1.put("type", "recommend");
        type1.put("disabled", false);

        type2.put("name", "同城");
        type2.put("type", "sameCity");
        type2.put("disabled", false);

        type3.put("name", "官方动态");
        type3.put("type", "official");
        type3.put("disabled", false);

        type4.put("name", "我的");
        type4.put("type", "mine");
        type4.put("disabled", false);

        mapList.add(type1);
        mapList.add(type2);
        mapList.add(type3);
        mapList.add(type4);
    }

    @Anonymous
    @GetMapping("/tabType")
    public R tabType() {
        return success(mapList);
    }
}
